<?php

class m_vendor extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function get_new_id() {
        $params = array(date("ymd"));
        $sql = "SELECT CAST(MID(vendor_cd, 8,5) AS UNSIGNED) + 1 'last_number'
                FROM vendor
                WHERE MID(vendor_cd, 2,6) = ?
                ORDER BY vendor_cd DESC ";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            
            $strNumber = '';
            for ($index = strlen($result['last_number']); $index < 5; $index++) {
                $strNumber .= '0';
            }
            $strNumber .= $result['last_number'];
            return 'V'.date("ymd").$strNumber;
        } else {
            return 'V'.date("ymd").'00001';
        }
    }
    
    function get_count_all_vendor() {
        $sql = "SELECT COUNT(vendor_cd)'total' FROM vendor";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    function get_count_all_vendor_search($params) {
        $sql = "SELECT COUNT(*)'total' FROM vendor
                WHERE vendor_cd LIKE ? 
                AND vendor_name LIKE ? 
                AND vendor_address LIKE ? ";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    //cek data
    function get_count_all_vendor_by_cd($params) {
        $sql = "SELECT COUNT(*) 'total' FROM vendor WHERE vendor_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    //cek data
    function get_count_all_vendor_by_kode_nama($params) {
        $sql = "SELECT COUNT(*) 'total' FROM vendor WHERE vendor_cd = ? AND vendor_name = ? AND mdb =?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    }

    // get user profil
    function get_all_vendor_pagination($params) {
        $sql = "SELECT * FROM vendor
                ORDER BY vendor_cd
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_all_vendor_pagination_search($params) {
        $sql = "SELECT * FROM vendor
                WHERE vendor_cd LIKE ? 
                AND vendor_name LIKE ? 
                AND vendor_address LIKE ?
                ORDER BY vendor_name
                LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_all_vendor() {
        $sql = "SELECT * FROM vendor
                ORDER BY vendor_name";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_vendor_by_cd($params) {
        $sql = "SELECT * FROM vendor WHERE vendor_cd = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    function get_vendor_by_name($params) {
        $sql = "SELECT * FROM vendor WHERE vendor_name = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }

    // insert
    function insert($user_id) {
        // update by this date
        $sql = "INSERT INTO vendor VALUES (?, ?, ?, ?, ?, ?, NOW())";
        return $this->db->query($sql, $user_id);
    }

    // edit
    function edit($user_id) {
        // update by this date
        $sql = "UPDATE vendor SET vendor_name = ?,  vendor_contact = ?, vendor_phone = ?, vendor_address = ?, mdb = ? WHERE vendor_cd = ?";
        return $this->db->query($sql, $user_id);
    }

    // delete
    function delete($user_id) {
        // update by this date
        $sql = "DELETE FROM vendor WHERE vendor_cd = ?";
        return $this->db->query($sql, $user_id);
    }

}
